package com.favccxx.search.api;

import com.favccxx.search.application.ConceptModelService;
import com.favccxx.search.application.SearchService;
import com.favccxx.search.domain.conceptmodel.ConceptModel;
import com.favccxx.search.domain.conceptmodel.ConceptModelRepo;
import com.favccxx.search.infrastructure.constant.AppConstants;
import com.favccxx.search.infrastructure.exception.ExceptionCode;
import com.favccxx.search.infrastructure.util.rest.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author favccxx
 */
@Slf4j
@Api(tags = "6.0 Search Interface")
@RequestMapping("/search")
@RestController
public class SearchController {

    @Autowired
    SearchService searchService;
    @Autowired
    ConceptModelService conceptModelService;

    @ApiOperation("Get concept model data by condition")
    @GetMapping("/searchData")
    public RestResult searchData(
            @RequestParam(value = "projectId", required = true) String projectId,
            @RequestParam(value = "conceptModelCode", required = true) String conceptModelCode,
            @RequestParam(value = "fields", required = false) String fields,
            @RequestParam(value = "condition", required = false) String condition,
            @RequestParam(value = "order", defaultValue = "", required = false) String order) {

        ConceptModel conceptModel = conceptModelService.findByProjectIdAndCode(projectId, conceptModelCode);
        if (conceptModel == null) {
            return RestResult.error(ExceptionCode.Search.INVALID_SEARCH_PARAM, "Search failed, projectId or conceptModeCode invalid.");
        }

        List<String> fieldList = new ArrayList<String>();
        if (StringUtils.isEmpty(fields) || AppConstants.DEFAULT_ALL.equalsIgnoreCase(fields) || AppConstants.DEFAULT_SQL_ALL.equalsIgnoreCase(fields)) {
            fieldList = conceptModel.getModelFields().keySet().stream().filter(
                    StringUtils::isNotBlank)
                    .collect(Collectors.toList());
        } else {
            fieldList = Arrays.asList(fields.split(","));
        }
        String data = searchService.searchData(projectId, conceptModelCode, fieldList, condition, order);
        return RestResult.sucess(data);

    }

   /* @ApiOperation("Get concept model data by condition")
    @GetMapping("/searchData")
    public RestResult searchData(
            @RequestParam(value = "conceptModelId", required = true) String conceptModelId,
            @RequestParam(value = "fields", required = false) String fields,
            @RequestParam(value = "condition", required = false) String condition,
            @RequestParam(value = "order", defaultValue = "", required = false) String order) {
        String data = searchService.searchData(conceptModelId, fields, condition, order);
        return RestResult.sucess(data);

    }*/

}
